import {createRouter, createWebHashHistory, RouteRecordRaw} from "vue-router"
import {workDocumentRoute} from "./workDocument"
import {workSpaceRoute} from "./workSpace"
import {userNoticeRoute} from "./userNotice"
import {projectRoute} from "./project"
import {groupRoute} from "./group"
import {profileRoute} from "@/router/profile";
import {useMetaLeft} from "@/hooks/use-meta-left";
import {getToken} from "@/util";

const routes: RouteRecordRaw[] = [
    {
        path: "/",
        redirect: "/login"
    },
    {
        path: "/login",
        name: "Login",
        component: () => import("@/pages/userLogin/UserLogin.vue")
    },
    {
        path: "/layout",
        name: "Layout",
        component: () => import("@/layout/Layout.vue"),
        children: [
            {
                path: "/layout",
                redirect: "/workspace"
            },
            ...workSpaceRoute,
            ...workDocumentRoute,
            ...userNoticeRoute,
            ...projectRoute,
            ...groupRoute,
            ...profileRoute
        ]
    }
]

const router = createRouter({
    routes,
    history: createWebHashHistory()
})

export const {left, setLeft} = useMetaLeft("workspace")
export const {left: subLeft, setLeft: setSubLeft} = useMetaLeft("myCreated")
router.beforeEach((to) => {
    const token = getToken()
    if (to.path !== "/login") {
        if (!token) {
            return "/login"
        }
    } else {
        if (token !== "") {
            return "/workspace"
        }
    }

    setLeft(to.meta.left)
    if (to.meta.subLeft !== undefined) {
        setSubLeft(to.meta.subLeft)
    }
})


export default router